﻿using Kavaa.Core.Data;
using Kavaa.Modules.PortailMedical.Entity;
using Kavaa.Modules.PortailMedical.Datalayer;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace Kavaa.Modules.PortailMedical.Datalayer
{
    internal static class PatientDAO
    {

        public static int AddPatient(Patient patient)
        {
            int ID = 0;
            foreach (var row in
            SqlDaoAdapter.ExecuteReader(ConfigurationCache.DatabaseConnectionString,
                  "[dbo].[sp_PortailMedical_AddPatient]"
            , new List<SqlParameter>() { 
                 new SqlParameter("@PatientName", patient.Name),
                  new SqlParameter("@PatientPrenom", patient.Prenom),
                   new SqlParameter("@PatientAge", patient.Age),
                new SqlParameter("@MutuelleId", patient.Mutuelle.Id),
               new SqlParameter("@PatientCIN", patient.CIN),
            }))
            { ID = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0; }

            return ID;
            
        }

        public static Reservation GetInfoConvocation(int ReservId)
        {
            Reservations reserv = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetInfoConvocation]"
            , new List<SqlParameter>() { 
                new SqlParameter("@ReservId", ReservId),
            }))
            {
                reserv.Add(new Reservation() 
                {
                    Id = (row["Id"] != DBNull.Value) ? Convert.ToInt32(row["Id"]) : 0,
                    Clinique = new Clinique() { Name = (row["CliniqueName"] != DBNull.Value) ? Convert.ToString(row["CliniqueName"]) : null },
                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    Medecin = new Medecin() { Name = (row["MedecinName"] != DBNull.Value) ? Convert.ToString(row["MedecinName"]) : null, Prenom = (row["MedecinPrenom"] != DBNull.Value) ? Convert.ToString(row["MedecinPrenom"]) : null },
                    Patient = new Patient() { Name = (row["NomPatient"] != DBNull.Value) ? Convert.ToString(row["NomPatient"]) : null, Prenom = (row["PrenomPatient"] != DBNull.Value) ? Convert.ToString(row["PrenomPatient"]) : null, CIN = (row["CINPatient"] != DBNull.Value) ? Convert.ToString(row["CINPatient"]) : null },
                    DateDebut = (row["DDebReserv"] != DBNull.Value) ? Convert.ToDateTime(row["DDebReserv"]) : DateTime.Now,
                    DateFin = (row["DFinReserv"] != DBNull.Value) ? Convert.ToDateTime(row["DFinReserv"]) : DateTime.Now,

                });
            }
            if (reserv.Count != 0)
            {
                return reserv.ElementAt(0);
            }
            else return new Reservation();
        }
    }
}
